#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define DEBUG 0
#define NOISY_TEST 1

int checkPlayCard(int p, struct gameState *post) {
  int r = 0;
  int gardensPos = -1 ;
  int i = 0;

  int coin_bonus = 0;

  //printf ("%d", (*post).handCount[p]);
  //printf("%d", post->hand[p][0]);
  for (i = 0; i < (*post).handCount[p]; i++) {
    if (post->hand[p][i] == gardens) {
      gardensPos = i;   
      r = playCard (gardensPos, -1, -1, -1, post);
      return 0; 
      }
  }
  while (post->hand[p][i] != gardens) {
     drawCard(p, post);
     
     i++;
 
  }
  post->phase = 0;
  post->numActions = 1;

  gardensPos = i;
  //r = playCard (council_roomPos, -1, -1, -1, post);
  //cardEffect( post->hand[p][council_roomPos], -1, -1, -1, post, council_roomPos, &coin_bonus);
  //printf("%d", 12);
  r = cardEffect(village, -1, -1,-1,post, 0, &coin_bonus);
  //printf("%d", 13);
  //assert (r == -1);

}

int main () {

  int i, n, r, p, deckCount, discardCount, handCount;

  int k[10] = {adventurer, council_room, feast, gardens, mine,
	       remodel, smithy, village, baron, great_hall};

  struct gameState G;

  printf ("Testing drawCard.\n");

  printf ("RANDOM TESTS.\n");

  SelectStream(2);
  PutSeed(3);
  
  for (n = 0; n < 2000; n++) {
    for (i = 0; i < sizeof(struct gameState); i++) {
      ((char*)&G)[i] = floor(Random() * 256);
    }
    p = floor(Random() * 2);
    r = initializeGame(2, k, 1, &G);
    G.deckCount[p] = floor(1+Random() * MAX_DECK);
    G.discardCount[p] = floor(1+Random() * MAX_DECK);
    G.handCount[p] = floor(1+Random() * MAX_HAND);
    //printf("%d", G.handCount[p]);
    //drawCard(p, &G);
    //G.numActions=G.numActions +2;
    G.hand[p][0] = village;
    //G.playedCardCount = 0;
    //discardCard(0, p, &G, 0);
    //discardCard(
    //G.playedCards[G.playedCardCount] = G.hand[p][0];
    // G.playedCardCount++;  
    //G.hand[p][0] = -1;
    //G.handCount[p]--;

    checkPlayCard(p, &G);
  }

  printf ("ALL TESTS OK\n");

  exit(0);

  printf ("SIMPLE FIXED TESTS.\n");
  for (p = 0; p < 2; p++) {
    for (deckCount = 0; deckCount < 5; deckCount++) {
      for (discardCount = 0; discardCount < 5; discardCount++) {
	for (handCount = 0; handCount < 5; handCount++) {
	  memset(&G, 23, sizeof(struct gameState)); 
	  r = initializeGame(2, k, 1, &G);
	  G.deckCount[p] = deckCount;
	  memset(G.deck[p], 0, sizeof(int) * deckCount);
	  G.discardCount[p] = discardCount;
	  memset(G.discard[p], 0, sizeof(int) * discardCount);
	  G.handCount[p] = handCount;
	  memset(G.hand[p], 0, sizeof(int) * handCount);
	  checkPlayCard(p, &G);
	}
      }
    }
  }

  return 0;
}
